<?php 
/**
* Test adding existing and uploaded file.
*/
class DomArHistoryTestCase extends DomArTestUnitTestCase {
	
	public function testBasics() {
		
		DomArHistoryTestClass_Item::getTable()->update();
		
		$u1 = new DomArHistoryTestClass_User();
		$u1->username = 'Balduran';
		$u1->save();
		
		$u2 = new DomArHistoryTestClass_User();
		$u2->username = 'Oliver';
		$u2->save();
		
		$a = new DomArHistoryTestClass_Item();
		$a->title = 'My title';
		$a->description = 'My description';
		$a->user = $u1;
		$a->save();
		
		$a->title = 'My changed title';
		$a->description = 'My changed description';
		$a->user = $u2;
		$a->save();
		
		$a->title = 'My changed title again';
		$a->description = 'My changed description again';
		$a->user = $u2;
		$a->save();
		
		// start testing
		
		DomAr::flushStoredObjects();
		$a = DomArHistoryTestClass_Item::load( $a->id );
		
		$this->assertIdentical( $a->history->getLastVersionNr(), 2 );
		$history = $a->history->query()->orderBy('t.version')->commit()->toArray();
		$this->assertIdentical( $history[0]->version, 1);
		$this->assertIdentical( $history[0]->title, 'My title');
		$this->assertIdentical( $history[0]->description, 'My description');
		$this->assertIdentical( $history[0]->user, $u1->id );
		$this->assertIdentical( $history[1]->version, 2);
		$this->assertIdentical( $history[1]->title, 'My changed title');
		$this->assertIdentical( $history[1]->description, 'My changed description');
		$this->assertIdentical( $history[1]->user, $u2->id );
		$this->assertIdentical( count($history), 2);
		
		$a->history->restoreFrom( 1 );
		$this->assertIdentical( $a->history->getLastVersionNr(), 2 );
		$history = $a->history->query()->orderBy('t.version')->commit()->toArray();
		$this->assertIdentical( $history[0]->version, 1);
		$this->assertIdentical( $history[0]->title, 'My title');
		$this->assertIdentical( $history[0]->description, 'My description');
		$this->assertIdentical( $history[0]->user, $u1->id );
		$this->assertIdentical( $history[1]->version, 2);
		$this->assertIdentical( $history[1]->title, 'My changed title');
		$this->assertIdentical( $history[1]->description, 'My changed description');
		$this->assertIdentical( $history[1]->user, $u2->id );
		$this->assertIdentical( count($history), 2);
		$this->assertIdentical( $a->title, 'My title');
		$this->assertIdentical( $a->description, 'My description');
		$this->assertIdentical( $a->user->id, $u1->id );
		
		$a->save();
		
		$this->assertIdentical( $a->history->getLastVersionNr(), 3 );
		$history = $a->history->query()->orderBy('t.version')->commit()->toArray();
		$this->assertIdentical( $history[0]->version, 1);
		$this->assertIdentical( $history[0]->title, 'My title');
		$this->assertIdentical( $history[0]->description, 'My description');
		$this->assertIdentical( $history[0]->user, $u1->id );
		$this->assertIdentical( $history[1]->version, 2);
		$this->assertIdentical( $history[1]->title, 'My changed title');
		$this->assertIdentical( $history[1]->description, 'My changed description');
		$this->assertIdentical( $history[1]->user, $u2->id );
		$this->assertIdentical( $history[2]->version, 3);
		$this->assertIdentical( $history[2]->title, 'My changed title again');
		$this->assertIdentical( $history[2]->description, 'My changed description again');
		$this->assertIdentical( $history[2]->user, $u2->id );
		$this->assertIdentical( count($history), 3);
		$this->assertIdentical( $a->title, 'My title');
		$this->assertIdentical( $a->description, 'My description');
		$this->assertIdentical( $a->user->id, $u1->id );
		
		$a->history->restoreFrom( 3 );
		$a->save();
		
		$this->assertIdentical( $a->history->getLastVersionNr(), 4 );
		$history = $a->history->query()->orderBy('t.version')->commit()->toArray();
		$this->assertIdentical( $history[0]->version, 1);
		$this->assertIdentical( $history[0]->title, 'My title');
		$this->assertIdentical( $history[0]->description, 'My description');
		$this->assertIdentical( $history[0]->user, $u1->id );
		$this->assertIdentical( $history[1]->version, 2);
		$this->assertIdentical( $history[1]->title, 'My changed title');
		$this->assertIdentical( $history[1]->description, 'My changed description');
		$this->assertIdentical( $history[1]->user, $u2->id );
		$this->assertIdentical( $history[2]->version, 3);
		$this->assertIdentical( $history[2]->title, 'My changed title again');
		$this->assertIdentical( $history[2]->description, 'My changed description again');
		$this->assertIdentical( $history[2]->user, $u2->id );
		$this->assertIdentical( $history[3]->version, 4);
		$this->assertIdentical( $history[3]->title, 'My title');
		$this->assertIdentical( $history[3]->description, 'My description');
		$this->assertIdentical( $history[3]->user, $u1->id );
		$this->assertIdentical( count($history), 4);
		$this->assertIdentical( $a->title, 'My changed title again');
		$this->assertIdentical( $a->description, 'My changed description again');
		$this->assertIdentical( $a->user->id, $u2->id );
		
		
		
		$ver2 = $a->history->getVersion(2);
		$this->assertIdentical( $ver2->title, 'My changed title');
		$this->assertIdentical( $ver2->changedPropertyNames, array('title'=>true,'description'=>true));
		
		$lastVer = $a->history->getLastVersion();
		$this->assertIdentical( $lastVer->title, 'My title');
		$this->assertIdentical( $lastVer->changedPropertyNames, array('title'=>true,'description'=>true,'user'=>true));
		
		$lastVerNr = $a->history->getLastVersionNr();
		$this->assertIdentical( $lastVerNr, 4 );
		
	}
	

	
}
?>